home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / HELP.C < prev    next >
C/C++ Source or Header  |  1991-02-16  |  7KB  |  271 lines

  1. /* Help functions for MicroGnuEmacs 2 */
  2.  
  3. #include "def.h"
  4.  
  5. #ifndef NO_HELP
  6. #include "kbd.h"
  7. #include "key.h"
  8. #ifndef NO_MACRO
  9. #include "macro.h"
  10. #endif
  11. extern int rescan();
  12.  
  13. /*
  14.  * Read a key from the keyboard, and look it
  15.  * up in the keymap.  Display the name of the function
  16.  * currently bound to the key.
  17.  */
  18. /*ARGSUSED*/
  19. desckey(f, n)
  20. {
  21.     register KEYMAP *curmap;
  22.     register PF funct;
  23.     register char *pep;
  24.     char prompt[80];
  25.     int c;
  26.     int m;
  27.     int i;
  28.  
  29. #ifndef NO_MACRO
  30.     if(inmacro) return TRUE;        /* ignore inside keyboard macro */
  31. #endif
  32.     (VOID) strcpy(prompt, "Describe key briefly: ");
  33.     pep = prompt + strlen(prompt);
  34.     key.k_count = 0;
  35.     m = curbp->b_nmodes;
  36.     curmap = curbp->b_modes[m]->p_map;
  37.     for(;;) {
  38.     for(;;) {
  39.         ewprintf("%s", prompt);
  40.         pep[-1] = ' ';
  41.         pep = keyname(pep, key.k_chars[key.k_count++] = c = getkey(FALSE));
  42.         if((funct = doscan(curmap, c)) != prefix) break;
  43.         *pep++ = '-';
  44.         *pep = '\0';
  45.         curmap = ele->k_prefmap;
  46.     }
  47.     if(funct != rescan) break;
  48.     if(ISUPPER(key.k_chars[key.k_count-1])) {
  49.         funct = doscan(curmap, TOLOWER(key.k_chars[key.k_count-1]));
  50.         if(funct == prefix) {
  51.         *pep++ = '-';
  52.         *pep = '\0';
  53.         curmap = ele->k_prefmap;
  54.         continue;
  55.         }
  56.         if(funct != rescan) break;
  57.     }
  58. nextmode:
  59.     if(--m < 0) break;
  60.     curmap = curbp->b_modes[m]->p_map;
  61.     for(i=0; i < key.k_count; i++) {
  62.         funct = doscan(curmap, key.k_chars[i]);
  63.         if(funct != prefix) {
  64.         if(i == key.k_count - 1 && funct != rescan) goto found;
  65.         funct = rescan;
  66.         goto nextmode;
  67.         }
  68.         curmap = ele->k_prefmap;
  69.     }
  70.     *pep++ = '-';
  71.     *pep = '\0';
  72.     }
  73. found:
  74.     if(funct == rescan) ewprintf("%k is not bound to any function");
  75.     else if((pep = function_name(funct)) != NULL)
  76.         ewprintf("%k runs the command %s", pep);
  77.     else    ewprintf("%k is bound to an unnamed function");
  78.     return TRUE;
  79. }
  80.  
  81. /*
  82.  * This function creates a table, listing all
  83.  * of the command keys and their current bindings, and stores
  84.  * the table in the *help* pop-up buffer.  This
  85.  * lets MicroGnuEMACS produce it's own wall chart.
  86.  */
  87. static BUFFER    *bp;
  88. static char buf[80];    /* used by showall and findbind */
  89.  
  90. /*ARGSUSED*/
  91. wallchart(f, n)
  92. {
  93.     int m;
  94.     static char locbind[80] = "Local keybindings for mode ";
  95.     int showall();
  96.  
  97.     bp = bfind("*help*", TRUE);
  98.     if (bclear(bp) != TRUE) return FALSE;    /* Clear it out.    */
  99.     for(m=curbp->b_nmodes; m > 0; m--) {
  100.         (VOID) strcpy(&locbind[27], curbp->b_modes[m]->p_name);
  101.         (VOID) strcat(&locbind[27], ":");
  102.         if((addline(bp, locbind) == FALSE) ||
  103.         (showall(buf, curbp->b_modes[m]->p_map) == FALSE) ||
  104.         (addline(bp, "") == FALSE)) return FALSE;
  105.     }
  106.     if((addline(bp, "Global bindings:") == FALSE) ||
  107.         (showall(buf, map_table[0].p_map) == FALSE)) return FALSE;
  108.     return popbuftop(bp);
  109. }
  110.  
  111. static    int showall(ind, map)
  112. char    *ind;
  113. KEYMAP    *map;
  114. {
  115.     register MAP_ELEMENT *ele;
  116.     register int i;
  117.     PF    functp;
  118.     char    *cp;
  119.     char    *cp2;
  120.     int    last;
  121.  
  122.     if(addline(bp, "") == FALSE) return FALSE;
  123.     last = -1;
  124.     for(ele = &map->map_element[0]; ele < &map->map_element[map->map_num] ; ele++) {
  125.         if(map->map_default != rescan && ++last < ele->k_base) {
  126.         cp = keyname(ind, last);
  127.         if(last < ele->k_base - 1) {
  128.             (VOID) strcpy(cp, " .. ");
  129.             cp = keyname(cp + 4, ele->k_base - 1);
  130.         }
  131.         do { *cp++ = ' '; } while(cp < &buf[16]);
  132.         (VOID) strcpy(cp, function_name(map->map_default));
  133.         if(addline(bp, buf) == FALSE) return FALSE;
  134.         }
  135.         last = ele->k_num;
  136.         for(i=ele->k_base; i <= last; i++) {
  137.         functp = ele->k_funcp[i - ele->k_base];
  138.         if(functp != rescan) {
  139.             if(functp != prefix) cp2 = function_name(functp);
  140.             else cp2 = map_name(ele->k_prefmap);
  141.             if(cp2 != NULL) {
  142.             cp = keyname(ind, i);
  143.             do { *cp++ = ' '; } while(cp < &buf[16]);
  144.             (VOID) strcpy(cp, cp2);
  145.             if (addline(bp, buf) == FALSE) return FALSE;
  146.             }
  147.         }
  148.         }
  149.     }
  150.     for(ele = &map->map_element[0]; ele < &map->map_element[map->map_num]; ele++) {
  151.         if(ele->k_prefmap != NULL) {
  152.         for(i = ele->k_base; ele->k_funcp[i - ele->k_base] != prefix; i++) {
  153.             if(i >= ele->k_num)  /* damaged map */
  154.             return FALSE;
  155.         }
  156.         cp = keyname(ind, i);
  157.         *cp++ = ' ';
  158.         if(showall(cp, ele->k_prefmap) == FALSE) return FALSE;
  159.         }
  160.     }
  161.     return TRUE;
  162. }
  163.  
  164. help_help(f, n)
  165. int f, n;
  166. {
  167.     KEYMAP *kp;
  168.     PF    funct;
  169.  
  170.     if((kp = name_map("help")) == NULL) return FALSE;
  171.     ewprintf("a b c: ");
  172.     do {
  173.     funct = doscan(kp, getkey(FALSE));
  174.     } while(funct==NULL || funct==help_help);
  175. #ifndef NO_MACRO
  176.     if(macrodef && macrocount < MAXMACRO) macro[macrocount-1].m_funct = funct;
  177. #endif
  178.     return (*funct)(f, n);
  179. }
  180.  
  181. static char buf2[128];
  182. static char *buf2p;
  183.  
  184. /*ARGSUSED*/
  185. apropos_command(f, n)
  186. int f, n;
  187. {
  188.     register char *cp1, *cp2;
  189.     char string[32];
  190.     FUNCTNAMES *fnp;
  191.     BUFFER *bp;
  192.     VOID findbind();
  193.  
  194.     if(eread("apropos: ", string, sizeof(string), EFNEW) == ABORT) return ABORT;
  195.     /* FALSE means we got a 0 character string, which is fine */
  196.     bp = bfind("*help*", TRUE);
  197.     if(bclear(bp) == FALSE) return FALSE;
  198.     for(fnp = &functnames[0]; fnp < &functnames[nfunct]; fnp++) {
  199.     for(cp1 = fnp->n_name; *cp1; cp1++) {
  200.         cp2 = string;
  201.         while(*cp2 && *cp1 == *cp2)
  202.         cp1++, cp2++;
  203.         if(!*cp2) {
  204.         (VOID) strcpy(buf2, fnp->n_name);
  205.         buf2p = &buf2[strlen(buf2)];
  206.         findbind(fnp->n_funct, buf, map_table[0].p_map);
  207.         if(addline(bp, buf2) == FALSE) return FALSE;
  208.         break;
  209.         } else cp1 -= cp2 - string;
  210.     }
  211.     }
  212.     return popbuftop(bp);
  213. }
  214.  
  215. static VOID findbind(funct, ind, map)
  216. PF funct;
  217. char *ind;
  218. KEYMAP    *map;
  219. {
  220.     register MAP_ELEMENT *ele;
  221.     register int i;
  222.     char    *cp;
  223.     int        last;
  224.     VOID bindfound();
  225.  
  226.     last = -1;
  227.     for(ele = &map->map_element[0]; ele < &map->map_element[map->map_num]; ele++) {
  228.     if(map->map_default == funct && ++last < ele->k_base) {
  229.         cp = keyname(ind, last);
  230.         if(last < ele->k_base - 1) {
  231.         (VOID) strcpy(cp, " .. ");
  232.         (VOID) keyname(cp + 4, ele->k_base - 1);
  233.         }
  234.         bindfound();
  235.     }
  236.     last = ele->k_num;
  237.     for(i=ele->k_base; i <= last; i++) {
  238.         if(funct == ele->k_funcp[i - ele->k_base]) {
  239.         if(funct == prefix) {
  240.             cp = map_name(ele->k_prefmap);
  241.             if(strncmp(cp, buf2, strlen(cp)) != 0) continue;
  242.         }
  243.         (VOID) keyname(ind, i);
  244.         bindfound();
  245.         }
  246.     }
  247.     }
  248.     for(ele = &map->map_element[0]; ele < &map->map_element[map->map_num]; ele++) {
  249.     if(ele->k_prefmap != NULL) {
  250.         for(i = ele->k_base; ele->k_funcp[i - ele->k_base] != prefix; i++) {
  251.         if(i >= ele->k_num) return; /* damaged */
  252.         }
  253.         cp = keyname(ind, i);
  254.         *cp++ = ' ';
  255.         findbind(funct, cp, ele->k_prefmap);
  256.     }
  257.     }
  258. }
  259.  
  260. static VOID bindfound() {
  261.     if(buf2p < &buf2[32]) {
  262.     do { *buf2p++ = ' '; } while(buf2p < &buf2[32]);
  263.     } else {
  264.     *buf2p++ = ',';
  265.     *buf2p++ = ' ';
  266.     }
  267.     (VOID) strcpy(buf2p, buf);
  268.     buf2p += strlen(buf);
  269. }
  270. #endif
  271.